package com.tx.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;

/**
 * @author cyt
 * @create 2020-08-01 20:38
 * @EnableWebMvc
 */
@EnableTransactionManagement
@ComponentScan(basePackages = {
        "com.tx"
})
@MapperScan(basePackages = {
        "com.tx.mapper"
})
@Configuration
public class DataBseConfig {
    /**
     * 配置数据源
     *
     * @return
     */
    @Bean
    public DruidDataSource dataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("cui0411..");
        druidDataSource.setUrl("jdbc:mysql://127.0.0.1:3306/ssm?characterEncoding=utf-8&serverTimezone=GMT&useSSL=false");
        druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        return druidDataSource;
    }

    /**
     * 配置事务管理器
     *
     * @param dataSource
     * @return
     */
    @Bean
    public DataSourceTransactionManager dataSourceTransactionManager(DruidDataSource dataSource) {
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        /**
         * 添加我们正在使用的数据源到 事务管理器中
         */
        dataSourceTransactionManager.setDataSource(dataSource);
        return dataSourceTransactionManager;
    }

    /**
     * 配置 mybatis plus
     *
     * @param dataSource
     * @return
     * @throws IOException
     */
//    @Bean
//    public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean(@Autowired DruidDataSource dataSource) throws IOException {
//        //配置 mybatis plus
//        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
//        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:UserMapper.xml"));
//        // 导入mybatis配置
//        MybatisConfiguration configuration = new MybatisConfiguration();
//        configuration.setJdbcTypeForNull(JdbcType.NULL);
//        configuration.setMapUnderscoreToCamelCase(true);
//        configuration.setCacheEnabled(false);
//        // 配置打印sql语句
//        configuration.setLogImpl(StdOutImpl.class);
//        sqlSessionFactoryBean.setConfiguration(configuration);
//        sqlSessionFactoryBean.setDataSource(dataSource);
//
//        return sqlSessionFactoryBean;
//    }

    /**
     * 配置 mybatis
     *
     * @param dataSource
     * @return
     * @throws IOException
     */
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(@Autowired DruidDataSource dataSource) throws IOException {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Interceptor[] plugins = new PageInterceptor[1];

        //配置分页插件
        PageInterceptor pageInterceptor = new PageInterceptor();
        Properties props = new Properties();
        props.setProperty("helperDialect", "mysql");
        props.setProperty("supportMethodsArguments", "true");
        props.setProperty("rowBoundsWithCount", "true");
        pageInterceptor.setProperties(props);
        ArrayList<PageInterceptor> pageInterceptors = new ArrayList<PageInterceptor>();
        pageInterceptors.add(pageInterceptor);

        factory.setPlugins(pageInterceptors.toArray(plugins));

        factory.setConfigLocation(resolver.getResource("classpath:mybatis.xml"));
        factory.setMapperLocations(resolver.getResources("classpath:UserMapper.xml"));
        factory.setDataSource(dataSource);
        return factory;
    }

}
